package com.party.core.service.crowdfund.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.crowdfund.ProjectBrokerageReadDao;
import com.party.core.dao.write.crowdfund.ProjectBrokerageWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.crowdfund.ProjectBrokerage;
import com.party.core.service.crowdfund.IProjectBrokerageService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 众筹和佣金
 * @Date:Created in 2017/10/25 0025 上午 10:07.
 */
@Service
public class ProjectBrokerageService implements IProjectBrokerageService {
    @Autowired
    private ProjectBrokerageReadDao projectBrokerageReadDao;
    @Autowired
    private ProjectBrokerageWriteDao projectBrokerageWriteDao;

    /**
     * 众筹佣金插入
     *
     * @param projectBrokerage 众筹佣金插入
     * @return 编号
     */
    @Override
    public String insert(ProjectBrokerage projectBrokerage) {
        BaseModel.preInsert(projectBrokerage);
        boolean result = projectBrokerageWriteDao.insert(projectBrokerage);
        if (result) {
            return projectBrokerage.getId();
        }
        return null;
    }

    /**
     * 更新众筹佣金
     *
     * @param projectBrokerage 众筹佣金
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(ProjectBrokerage projectBrokerage) {
        projectBrokerage.setUpdateDate(new Date());
        return projectBrokerageWriteDao.update(projectBrokerage);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectBrokerageWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectBrokerageWriteDao.delete(id);
    }

    /**
     * 批量逻辑删号
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectBrokerageWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectBrokerageWriteDao.batchDelete(ids);
    }

    /**
     * 获取众筹佣金
     *
     * @param id 主键
     * @return 众筹佣金
     */
    @Override
    public ProjectBrokerage get(String id) {
        return projectBrokerageReadDao.get(id);
    }

    /**
     * 分页查询
     *
     * @param projectBrokerage 众筹佣金
     * @param page             分页信息
     * @return 分页列表
     */
    @Override
    public List<ProjectBrokerage> listPage(ProjectBrokerage projectBrokerage, Page page) {
        return projectBrokerageReadDao.listPage(projectBrokerage, page);
    }

    /**
     * 查询所有
     *
     * @param projectBrokerage 众筹佣金
     * @return 分页列表
     */
    @Override
    public List<ProjectBrokerage> list(ProjectBrokerage projectBrokerage) {
        return projectBrokerageReadDao.listPage(projectBrokerage, null);
    }

    @Override
    public List<ProjectBrokerage> batchList(@NotNull Set<String> ids, ProjectBrokerage projectBrokerage, Page page) {
        return projectBrokerageReadDao.batchList(ids, new HashMap<String, Object>(), page);
    }

    @Override
    public ProjectBrokerage findByProjectId(String projectId) {
        ProjectBrokerage projectBrokerage = new ProjectBrokerage();
        projectBrokerage.setProjectId(projectId);
        return projectBrokerageReadDao.getUnique(projectBrokerage);
    }
}
